package logic

import (
	"MyBlogv2/blog-common/HttpCode"
	"MyBlogv2/blog-common/define"
	"MyBlogv2/blog-common/models"
	"context"
	"github.com/jinzhu/copier"

	"MyBlogv2/Servers/UserAction/internal/svc"
	"MyBlogv2/Servers/UserAction/userAction"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetTreeHoleListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetTreeHoleListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTreeHoleListLogic {
	return &GetTreeHoleListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetTreeHoleListLogic) GetTreeHoleList(in *UserAction.UserActionRequest) (*UserAction.GetTreeHoleListResponse, error) {
	var treeHoleVoList []models.TreeHoleVo
	models.Db.Model(new(models.TreeHole)).
		Table("t_tree_hole th").
		Select("th.*,u.nickname nickname,u.avatar avatar").
		Joins("left join sys_user u on th.user_id = u.id").
		Where("is_check = ?", define.IS_CHECK_YES).
		Order("created_at desc").
		Find(&treeHoleVoList)
	var treeHoleVoListResponse []*UserAction.TreeHoleVo

	copier.Copy(&treeHoleVoListResponse, &treeHoleVoList)

	return &UserAction.GetTreeHoleListResponse{
		Code: int64(HttpCode.SUCCESS),
		Data: treeHoleVoListResponse,
	}, nil
}
